using System;
using System.Collections.Generic;
using MvpFramework.DesignByContract;

namespace MvpFramework
{
    public class MvpViewLocationRepository : IMvpViewLocationRepository
    {
        private static MvpViewLocationRepository _instance;
        private readonly Dictionary<Type, string> _resolver;

        private const string navigationNotSetUpErrorMessageFormat =
            @"Navigation has not been setup for the view {0}
please add the navigation using MvpViewLocationRepository.Instance.AddLocationFor<VIEW>(string) method during Application Startup.";
        /// <summary>
        /// Singletone implementation
        /// </summary>
        public static MvpViewLocationRepository Instance
        {
            get
            {
                if(_instance == null)
                {
                    _instance = new MvpViewLocationRepository();
                }
                return _instance;
            }
        }

        public MvpViewLocationRepository()
        {
            _resolver = new Dictionary<Type, String>();
        }

        /// <summary>
        /// Adds a location to the repository
        /// </summary>
        /// <typeparam name="VIEW"></typeparam>
        /// <param name="location"></param>
        public void AddLocationFor<VIEW>(string location) where VIEW:IMvpView
        {
            _resolver.Add(typeof(VIEW),location);
        }
        /// <summary>
        /// returns the location for the View.
        /// </summary>
        /// <typeparam name="IMvpView"></typeparam>
        /// <returns></returns>
        public string GetLocationFor<IMvpView>()
        {
            var type = typeof (IMvpView);
            Check.Require(_resolver.ContainsKey(type),String.Format(navigationNotSetUpErrorMessageFormat,type));
            return _resolver[type];
        }

        
    }
}